//
//  main.m
//  05-遍历.排序
//
//  Created by Snow on 16/7/8.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    
    NSArray *arr = @[@"黄光亮", @"张亮", @"田亮", @"贾乃亮", @"高亮"];
    NSMutableArray *newArr = [NSMutableArray array];
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        [newArr addObject:arr[i]];
    }
    NSLog(@"%@", newArr);
    
    NSDictionary *dic = @{@"name": @"李四",
                           @"sex": @"male",
                           @"age": @"18"};
    NSArray *arr1 = [dic allKeys];
    for (NSInteger i = 0; i < arr1.count ; i++) {
        NSString *key = arr1[i];
        NSString *value = [dic objectForKey:key];
        NSLog(@"key = %@, value = %@", key, value);
    }
    
    /* 遍历方式 */
    //枚举器
    //NSEnumeration依赖于集合类型(数组,字典,集合), 不能单独使用
    NSEnumerator *stringArrayEnumerator = [arr objectEnumerator];
    
    id value = nil;
    while (value = [stringArrayEnumerator nextObject]) {
        NSLog(@"value = %@", value);
    }
    //倒叙枚举
    NSEnumerator *rever = [arr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [rever nextObject]) {
        
        NSLog(@"value1 = %@", value1);
    }
    
    //字典枚举器(字典无需, 没有倒叙枚举)
    //默认取出来的值, 是value
    NSEnumerator *dicEnumerator = [dic.allKeys objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicEnumerator nextObject]) {
        NSArray *keys = [dic allKeysForObject:dicValue];
        NSLog(@"%@-%@", keys, dicValue);
    }
    
    //集合枚举
    NSSet *set = [NSSet setWithObjects:@"110", @"223", @"119", @"112", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"%@", setValue);
    }
    
    //快速枚举
    //数组
    for (NSString *string in arr) {
        NSLog(@"%@", string);//是对
    }
    //将数组中的字符串拼接成一个字符串 用for-in
    NSString *stringOfComponent = @"";
    for (NSString *str in arr) {
        stringOfComponent = [stringOfComponent stringByAppendingString:str];
    }
    NSLog(@"%@", stringOfComponent);
    
    //字典
    for (NSString *key in dic) {
        NSLog(@"%@", [dic objectForKey:key]);
    }
    //快速枚举一个集合
    for (NSString *anyobject in set) {
        NSLog(@"%@", anyobject);
    }
    
    //排序
    // 参数1 key 排序条件
    // 参数2 acending:yes(升序) no(降序)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@13, @14, @15, nil];
    // 数组有排序的方法 (sortArrayUsingDescriptor)
    // 参数为一个排序条件的数组
    arr = [arr sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", arr);
    [mArr sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", mArr);
    
    // 排序1: 可变数组和不可变数组通过sort排序调用的方法不同
    // 1. 不可变数组, 排序后需要生成新的数组
    // 2. 可变数组, 排序直接在本身上修改
    
    Person *per = [[Person alloc] init];
    per.name = @"依依";
    per.age = 13;
    Person *per1 = [[Person alloc] init];
    per1.name = @"夜夜";
    per1.age = 18;
    Person *per2 = [[Person alloc] init];
    per2.name = @"秋秋";
    per2.age = 15;
    Person *per3 = [[Person alloc] init];
    per3.name = @"心心";
    per3.age = 19;
    Person *per4 = [[Person alloc] init];
    per4.name = @"莹莹";
    per4.age = 12;
    //根据name排序 根据age 排序
    NSArray *perArray = @[per, per1, per2, per3, per4];
    NSLog(@"排序前:%@", perArray);
    NSSortDescriptor *persort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    perArray = [perArray sortedArrayUsingDescriptors:@[persort]];
    NSLog(@"排序后:%@", perArray);
    
    //排序2
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTouch", @"iMac", @"Mac mini"];
    
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"sortString:%@", sortString);
    
    
    return 0;
}
